<template>
    <div v-if="readyShow">
        <h1 style="font-size:25px ;">{{name}}</h1>
        <el-button class="vote" type="primary" style="float: right;margin-right: 5%;margin-top: 5%;position: relative"
                   disabled>{{stateText}}
        </el-button>
        <br/>
        <span>表决类型： </span>
        <span>{{type}}</span><br/>
        <span>相关文件：</span>
        <el-table
                @cell-click="downloadFile"
                :data="fileList" width="100%">
            <el-table-column
                    fixed
                    prop="name"
                    label="文件">
            </el-table-column>
        </el-table>

        <div style="margin-top: 5%"></div>

        <div v-if="canVote === 1">
            <i v-show="showTip" class="el-icon-warning" style="display: block;margin-top:3%">
                预投票中：预投票结束，正式投票开始后，预投票结果将自动设为正式投票结果。 </i>
            <div v-if="commonResult === ''" style="margin-top: 5%">
                <div style="width: 100%; text-align: center" v-if="canVote == 1">
                <el-button @click="approval" type="primary" style="margin-right: 4%; width: 30%">赞成</el-button>
                <el-button @click="oppose" type="primary" style="margin-left: 4%; width: 30%">反对</el-button>
                <el-button @click="giveUp" type="warning" style="margin-left: 7%; width: 20%">放弃</el-button>
            </div>
                <div style="width: 100%; text-align: center" v-if="canVote == 0">
                    <el-button @click="approval" type="primary" style="margin-right: 4%; width: 30%" disabled>赞成
                    </el-button>
                    <el-button @click="oppose" type="primary" style="margin-left: 4%; width: 30%" disabled>反对
                    </el-button>
                    <el-button @click="giveUp" type="warning" style="margin-left: 7%; width: 20%" disabled>放弃
                    </el-button>
                </div>
            </div>
            <div v-if="commonResult === '1'" style="margin-top: 5%">
                <div style="width: 100%; text-align: center">
                    <el-button @click="voteAgainVisible = true" type="info" style="width: 80%">已赞成,点击重新投票</el-button>
                </div>
            </div>
            <div v-if="commonResult === '2'" style="margin-top: 5%">
                <div style="width: 100%; text-align: center">
                    <el-button @click="voteAgainVisible = true" type="info" style="width: 80%">已反对,点击重新投票</el-button>
                </div>
            </div>
            <div v-if="commonResult === '3'" style="margin-top: 5%">
                <div style="width: 100%; text-align: center">
                    <el-button @click="voteAgainVisible = true" type="info" style="width: 80%">已放弃,点击重新投票</el-button>
                </div>
            </div>


        </div>

        <div style="width: 100%; text-align: center;margin-top: 5%" v-if="certificateButton">
            <el-button @click="toCertificate" type="primary" style="width: 80%">查看表决存证</el-button>
        </div>
        <div v-if="commonResultList" style="margin-top: 5%">表决结果 ：
            <el-table

                    :data="groupList"
                    border
                    style="width: 100%">
                <el-table-column
                        fixed
                        prop="groupName"
                        label="组别"
                        width="90">
                </el-table-column>
                <el-table-column
                        prop="peopleSum"
                        label="总签到人数"
                        width="100">
                </el-table-column>
                <el-table-column
                        prop="peopleFavor"
                        label="赞成人数"
                        width="100">
                </el-table-column>
                <el-table-column
                        prop="peopleRate"
                        label="赞成人数比例"
                        width="100">
                </el-table-column>
                <el-table-column
                        prop="amountSum"
                        label="总债权额"
                        width="100">
                </el-table-column>
                <el-table-column
                        prop="amountFavor"
                        label="赞成债权额"
                        width="120">
                </el-table-column>
                <el-table-column
                        prop="amountRate"
                        label="赞成债权额比例"
                        width="120">
                </el-table-column>
            </el-table>
        </div>
        <div style="margin-top: 5%"></div>
        <div style="font-size: 12px;word-wrap: break-word;">文件下载地址 :
            <span>{{compressFileUrl}}</span>
        </div>

        <el-dialog
                title="重新投票"
                :visible.sync="voteAgainVisible"
                width="90%"
                center>
            <el-form ref="form" :model="form" label-width="80px">
                <el-form-item label="投票: ">
                    <el-radio-group v-model="form.vote">
                        <el-radio label="1" style="margin-top: 20%">赞成</el-radio><br/>
                        <el-radio label="2" style="margin-top: 20%">反对</el-radio> <br/>
                        <el-radio label="3" style="margin-top: 20%">放弃</el-radio> <br/>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="voteAgainVisible = false">取 消</el-button>
                <el-button type="primary" @click="voteAgain">确 定</el-button>
            </span>
        </el-dialog>

        <el-dialog
                :title=dialogTitle
                :visible.sync="voteDialogVisible"
                width="80%"
                center>
            <span slot="footer" class="dialog-footer">
                <el-button @click="voteDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="vote">确 定</el-button>
            </span>
        </el-dialog>

        <el-dialog
                title="提示"
                :visible.sync="submitCommitteeVisible"
                width="90%">
            <span>当前是第{{voteTimes +1}}次提交， 您总共可以提交2次</span>
            <span slot="footer" class="dialog-footer">
            <el-button @click="submitCommitteeVisible = false" style="margin-right: 5%">取 消</el-button>
            <el-button type="primary" @click="confirmVoteTime" style="margin-left: 5%">确 定</el-button>
          </span>
        </el-dialog>
    </div>
</template>

<script>
    import util from 'assets/js/util.js'
    import conf from 'assets/js/conf.js'


    export default {
        data() {
            return {
                fileList: [],
                tempCommonResult: 1,
                name: '',
                dialogTitle: '',
                type: '',
                index: -1,
                groupList: [],
                certificateButton: false,
                compressFileUrl: '',
                showTip: false,
                submitCommitteeVisible: false,
                centerDialogVisible: false,
                voteDialogVisible: false,
                voteAgainVisible: false,
                commonResult: "",
                form: {
                    vote: "1",
                },
                voteTimes: 0,
                canVote: 0,
                state: 0,
                bropState: 0,
                commonResultList: false,
                readyShow: false,
            }
        },
        mounted: function () {
            const _self = this;
            _self.voteId = util.getUrlParam(document.location.href, 'voteId');
            util.ajax({
                url: `/vote/info`,
                params: {voteId: util.getUrlParam(document.location.href, 'voteId')},
                success: function (data) {
                    if (data.data.state === 3) {
                        data.data.voteCommonResultList.forEach(_group => _self.groupList.push({
                            groupName: _group.groupName,
                            peopleFavor: _group.peopleFavor,
                            peopleSum: _group.peopleSum,
                            amountSum: _group.amountSum,
                            amountFavor: _group.amountFavor,
                            peopleRate: _group.peopleRate,
                            amountRate: _group.amountRate,
                            state: _group.state,
                        }));
                        _self.commonResultList = true;
                    }
                    _self.selectNum = data.data.selectNum;
                    _self.name = data.data.name;
                    _self.id = data.data.id;
                    _self.state = data.data.state;
                    _self.stateText = _self.matchStateText(data.data.state);
                    _self.commonResult = data.data.commonResult;
                    _self.materialFileids = data.data.materialFileids;
                    _self.compressFileUrl = data.data.compressFileUrl;
                    _self.type = _self.matchTypeText(data.data.type);
                    _self.voteTimes = data.data.voteTimes;
                    _self.canVote = data.data.canVote;
                    _self.bropState = data.data.bropState;

                    util.ajax({
                        url: '/file/getFileList',
                        params: {eids: _self.materialFileids},
                        success: function (data) {
                            data.data.forEach(_file => _self.fileList.push({
                                id: _file.id,
                                name: _file.name,
                            }))

                        },
                    });
                    if (_self.bropState !== 0 && _self.voteTimes >= 1) {
                        _self.certificateButton = true;
                    }
                    _self.readyShow = true;
                },
            });

        },
        methods: {
            /*deleteRow() {
                this.fileList.splice(this.index, 1);
                this.centerDialogVisible = false
            },
            showDialog: function (index) {
                this.index = index;
                this.centerDialogVisible = true
            },*/
            toCertificate: function () {
                window.location.href = '../meeting/certificate-detail.html?' + 'voteId=' + this.voteId;
            },

            approval: function () {
                if(this.canVote === 1 ){

                }
                this.dialogTitle = '是否赞成';
                this.submitCommitteeVisible = true;
                this.tempCommonResult = "1";
            },
            oppose: function () {
                this.dialogTitle = '是否反对';
                this.submitCommitteeVisible = true;
                this.tempCommonResult = "2";
            },
            giveUp: function () {
                 this.dialogTitle = '是否放弃';
                 this.submitCommitteeVisible = true;
                 this.tempCommonResult = "3";
            },
            confirmVoteTime: function () {
                this.submitCommitteeVisible = false;
                this.vote();
            },
            downloadFile: function (row, column, cell, event) {
                window.open(conf.baseUrl + '/file/get/' + row.id + '?type=1')
            },
            vote: function () {
                const _self = this;
                const loading = this.$loading({
                    lock: true,
                    text: 'Loading',
                    spinner: 'el-icon-loading',
                    background: 'rgba(0, 0, 0, 0.7)'
                });
                util.ajax({
                    url: `/vote/vote`,
                    params: {voteId: _self.voteId, result: _self.tempCommonResult},
                    success: function (data) {
                        _self.voteDialogVisible = false;
                        _self.voteAgainVisible = false;
                        _self.commonResult = _self.tempCommonResult;
                        _self.voteTimes++;
                        if (_self.bropState !== 0 && _self.voteTimes >= 1) {
                            _self.certificateButton = true;
                        }
                        loading.close();
                    },
                });
            },
            voteAgain: function () {
                this.tempCommonResult = this.form.vote;
                if (this.voteTimes <= 1)
                    this.submitCommitteeVisible = true;
                else
                    alert("您的投票次数已用完");
                // this.vote()
            },
            matchStateText: function (type) {
                let typeText = '';
                switch (type) {
                    case 0:
                        typeText = '预选举';
                        break;
                    case 1:
                        typeText = '选举中';
                        break;
                    case 2:
                        typeText = '线上投票关闭中';
                        break;
                    case 3:
                        typeText = '已结束';
                        break;
                    default:
                        break
                }
                return typeText
            },
            matchTypeText: function (type) {
                let typeText = '';
                switch (type) {
                    case 99:
                        typeText = '其他';
                        break;
                    case 1:
                        typeText = '继续或者停止债务人的营业';
                        break;
                    case 2:
                        typeText = '债务人重整计划';
                        break;
                    case 3:
                        typeText = '债务人财产的管理方案';
                        break;
                    case 4:
                        typeText = '破产财产的变价方案';
                        break;
                    case 5:
                        typeText = '和解协议';
                        break;
                    case 6:
                        typeText = '破产财产的分配方案';
                        break;
                    case 0:
                        typeText = '债权人委员会选举';
                        break;
                    default:
                        break
                }
                return typeText
            },
            matchCreditTypeText: function (type) {
                let typeText = '';
                switch (type) {
                    case 1:
                        typeText = '职工债权';
                        break;
                    case 2:
                        typeText = '普通债权';
                        break;
                    case 3:
                        typeText = '税收债权';
                        break;
                    case 4:
                        typeText = '优先债权';
                        break;
                    default:
                        break
                }
                return typeText
            },
        },
    }
</script>

<style scoped>
    h1 {
        margin: 10px;
    }

    /*.vote {
        position: absolute;
        margin: 10px;
        right: 20px;
        top: 0;
    }*/

    div[data-v-6ea9db14] {
        margin: 0;
    }

    span {
        line-height: 35px;
    }
</style>
